package com.example.administrator.nl.activity.appointment;

import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.nl.R;
import com.example.administrator.nl.adapter.AppointmentProjectAdapter;
import com.example.administrator.nl.base.BaseActivity;
import com.example.administrator.nl.bean.AppointmentProjectBean;
import com.example.administrator.nl.dialog.DialogAppointmentProjectView;
import com.example.administrator.nl.util.DataUtil;
import com.example.administrator.nl.util.ToastUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

// 预约项目
public class AppointmentProjectActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    // 页面标题
    @BindView(R.id.tv_title)
    TextView tv_title;

    // 返回键
    @BindView(R.id.iv_finish)
    ImageView iv_finish;

    // 日期
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;

    private DialogAppointmentProjectView dialog;

    private List<AppointmentProjectBean> list;

    private AppointmentProjectAdapter appointmentProjectAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        listener();
    }


    @Override
    public int getContentView() {
        return R.layout.activity_appointment_perject;
    }

    List<String> date;

    @Override
    public void init() {
        tv_title.setText("预约");
        list = getData();
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        appointmentProjectAdapter = new AppointmentProjectAdapter(R.layout.item_appointment_project_layout, list);

        View footView = LayoutInflater.from(this).inflate(R.layout.foot_appointment_project_layout, null);
        footLayout(footView);


        appointmentProjectAdapter.setFooterView(footView);
        recyclerView.setAdapter(appointmentProjectAdapter);
        // 获取7天之内的月日 包含今天
        date = DataUtil.getDate(7);
        // 获取7天之内的星期1--星期7 包含今天
        List<String> weekDays = DataUtil.getWeekDays(7);
        for (int i = 0; i < date.size(); i++) {
            if (i == 0) {
                tabLayout.addTab(tabLayout.newTab().setText("今天" + "\n" + date.get(i)));
            } else if (i == 1) {
                tabLayout.addTab(tabLayout.newTab().setText("明天" + "\n" + date.get(i)));
            } else {
                tabLayout.addTab(tabLayout.newTab().setText(weekDays.get(i) + "\n" + date.get(i)));
            }
        }
    }

    TagFlowLayout flowlayout;
    TextView tv_phone;
    TextView tv_project_number;
    TextView tv_project_date;
    TextView tv_submit;

    List<String> listProject;

    private void footLayout(View footView) {
        listProject = new ArrayList<>();
        listProject.add("单词漂浮");
        listProject.add("感受奇迹");
        listProject.add("全身spa");

        if (null != footView) {
            flowlayout = footView.findViewById(R.id.flowlayout);
            tv_phone = footView.findViewById(R.id.tv_phone);
            tv_project_number = footView.findViewById(R.id.tv_project_number);
            tv_project_date = footView.findViewById(R.id.tv_project_date);
            tv_submit = footView.findViewById(R.id.tv_submit);

            flowlayout.setMaxSelectCount(3);
            flowlayout.setAdapter(new TagAdapter<String>(listProject) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView tv = (TextView) LayoutInflater.from(AppointmentProjectActivity.this).inflate(R.layout.label_appointment_project,
                            null);
                    tv.setText(s);
                    return tv;
                }
            });

            // 搜索标签点击事件
            flowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {

                    tv_project_number.setText(flowlayout.getSelectedList().size() + "");
                    return true;

                }
            });

            tv_submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    dialog.show();
                }
            });

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_finish:
                finish();
                break;
            default:
                break;

        }
    }

    public List<AppointmentProjectBean> getData() {
        list = new ArrayList<>();
        list.add(new AppointmentProjectBean("0", "10: 00"));
        list.add(new AppointmentProjectBean("0", "11: 00"));
        list.add(new AppointmentProjectBean("1", "12: 00"));
        list.add(new AppointmentProjectBean("1", "13: 00"));
        list.add(new AppointmentProjectBean("1", "14: 00"));
        list.add(new AppointmentProjectBean("1", "15: 00"));
        list.add(new AppointmentProjectBean("1", "16: 00"));
        list.add(new AppointmentProjectBean("1", "17: 00"));
        list.add(new AppointmentProjectBean("1", "18: 00"));
        list.add(new AppointmentProjectBean("1", "19: 00"));
        list.add(new AppointmentProjectBean("1", "20: 00"));

        return list;
    }

    private void listener() {
        appointmentProjectAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.ll_line_appointment:

                        if ("0".equals(list.get(position).getMake())) {
                            ToastUtil.ToastShort("不可预约");

                        } else if ("1".equals(list.get(position).getMake())) {
                            // 更新list
                            updateList(position);
                            list.get(position).setMake("2");
                            tv_project_date.setText(date.get(0) + "" + list.get(position).getMakeTime());

                        } else if ("2".equals(list.get(position).getMake())) {
                            // 更新list
                            updateList(position);
                            list.get(position).setMake("2");
                            tv_project_date.setText(date.get(0) + "" + list.get(position).getMakeTime());

                        }
                        appointmentProjectAdapter.setNewData(list);
                        break;
                    default:
                        break;
                }
            }
        });

        dialog = new DialogAppointmentProjectView(AppointmentProjectActivity.this,
                R.layout.dialog_appointment_project_dialaog,
                new int[]{R.id.tv_content, R.id.tv_cancel, R.id.tv_submit});
        dialog.setOnCenterItemClickListener(new DialogAppointmentProjectView.OnCenterItemClickListener() {
            @Override
            public void OnCenterItemClick(DialogAppointmentProjectView dialog, View view) {
                switch (view.getId()) {

                    case R.id.tv_cancel:
                        ToastUtil.ToastShort("你点击了取消");
                        dialog.dismiss();
                        break;
                    case R.id.tv_submit:
                        ToastUtil.ToastShort("你点击了提交");
                        dialog.dismiss();
                        break;
                    default:
                        break;

                }
            }
        });

    }

    private void updateList(int selectIndex) {
        // 当前选中的list是否已经选过了，如已经选过了，就不处理该事件
        if (list.get(selectIndex).getMake().equals("2")) {
            return;
        }
        // 将所有不等于0的预约时间段都修改为1
        for (int i = 0; i < list.size(); i++) {

            if (!list.get(i).getMake().equals("0")) {
                list.get(i).setMake("1");

            }
        }
    }

}
